﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Data.Repository.Enum;
using Common.Service;
using Common.Service.DataTables;
using SMEsCustomer360Degree.Core.Configuration;
using SMEsCustomer360Degree.Models.ViewModels.CustomerManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SMEsCustomer360Degree.Controllers
{
    public class CustomerManagerController : ControllerBase
    {
        #region Customer
        [Authorize]
        public ActionResult TotalCustomer()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult TotalCustomerData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                int index = pageRequest.SearchColumns.FindIndex(x => !string.IsNullOrEmpty(x));
                RetrieveCustomerRequest request = new RetrieveCustomerRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentDao = CurrentUserRecord,
                    SearchIndex = index,
                    TextSearch = index != -1 ? pageRequest.SearchColumns[index] : string.Empty,
                };
                var customerRecords = new RetrieveCustomerResponse();
                customerRecords = serviceClient.Proxy.RetrieveTotalCustomer(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString()
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion
        #region Customer Portfolio
        [Authorize]
        public ActionResult CustomerPortfolio()
        {
            CustomerPortfolioViewModel listCountCustomer = CountCustomerByGroup();
            return View(listCountCustomer);
        }


        [Authorize]
        [HttpPost]
        public ActionResult CustomerPortfolioData(DataTablesPageRequest pageRequest, string type, string group, string grouptype)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                int index = pageRequest.SearchColumns.FindIndex(x => !string.IsNullOrEmpty(x));
                RetrieveCustomerRequest request = new RetrieveCustomerRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentDao = CurrentUserRecord,
                    Region = DataRegion.RegionMap(CurrentUserRecord.Zone.ToUpper()),
                    SearchIndex = index,
                    TextSearch = index != -1 ? pageRequest.SearchColumns[index] : string.Empty,
                    Type = type,
                    FromDate = DateTime.Now.AddMonths(-1),
                    Group = string.IsNullOrEmpty(group) || group.Equals("null") ? string.Empty : group,
                    GroupType = string.IsNullOrEmpty(grouptype) || grouptype.Equals("null") ? string.Empty : grouptype
                };
                var customerRecords = new RetrieveCustomerResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomer(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                if (type == "new_deposit")
                {
                    type = "deposit";
                }
                if (type == "new_loan")
                {
                    type = "loan";
                }
                if (type == "new_card")
                {
                    type = "card";
                }
                if (type == "new_i2b")
                {
                    type = "i2b";
                }
                if (type == "new_pos")
                {
                    type = "pos";
                }
                switch (type)
                {
                    case "deposit":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString(),
                                                             x.Balance,
                                                             x.Address,
                    }).ToList()
                        };
                        break;
                    case "loan":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString(),
                                                             x.Balance,
                                                             x.Address
                    }).ToList()
                        };
                        break;
                    case "card":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString(),
                                                             x.DebitCard,
                                                             x.CreditCard,
                                                             x.BalanceCredit,
                                                             x.Address
                    }).ToList()
                        };
                        break;
                    case "i2b":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString(),
                                                             x.NoUser,
                                                             x.Address
                    }).ToList()
                        };
                        break;
                    case "pos":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString(),
                                                             x.NoPos,
                                                             x.TransactionInMonth,
                                                             x.Address
                    }).ToList()
                        };
                        break;
                    default:
                        break;
                }


            }
            return Json(pageResponse);
        }
        #endregion

        #region Cutomer Priority
        [Authorize]
        public ActionResult CustPriority()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustPriorityData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                int index = pageRequest.SearchColumns.FindIndex(x => !string.IsNullOrEmpty(x));
                RetrieveCustomerPriorityRequest request = new RetrieveCustomerPriorityRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    SearchIndex = index,
                    TextSearch = index != -1 ? pageRequest.SearchColumns[index] : string.Empty
                };
                var customerRecords = new RetrieveCustomerPriorityResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerPriority(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;

                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString(),
                                                             x.CustType,
                                                             x.Address
                    }).ToList()
                };


            }
            return Json(pageResponse);
        }
        #endregion
        #region CustSubSegment
        [Authorize]
        public ActionResult CustSubSegment()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustSubSegmentData(DataTablesPageRequest pageRequest, string type)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                int index = pageRequest.SearchColumns.FindIndex(x => !string.IsNullOrEmpty(x));
                RetrieveCustomerSubSegmentRequest request = new RetrieveCustomerSubSegmentRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    Type = type,
                    SearchIndex = index,
                    TextSearch = index != -1 ? pageRequest.SearchColumns[index] : string.Empty,
                };
                var customerRecords = new RetrieveCustomerSubSegmentResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerSubSegment(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString()
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion
        #region New Customer
        [Authorize]
        public ActionResult NewCustomers()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult NewCustomerData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRequest request = new RetrieveCustomerRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentDao = CurrentUserRecord,
                    TextSearch = pageRequest.Search
                };
                var customerRecords = new RetrieveCustomerResponse();
                customerRecords = serviceClient.Proxy.RetrieveNewCustomer(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.DateOpenCif.ToShortDateString()
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region Alert

        [Authorize]
        public ActionResult CustomersAlert()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult AlertBadBank(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerBadBankRequest request = new RetrieveCustomerBadBankRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    TextSearch = pageRequest.Search
                };
                var customerRecords = new RetrieveCustomerBadBankResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerBadBank(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<AlertCustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CustName,
                                                             x.Industry,
                                                             x.MovingInDate
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }


        [Authorize]
        [HttpPost]
        public ActionResult AlertLoanMatdt(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerLoanMatdtRequest request = new RetrieveCustomerLoanMatdtRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    TextSearch = pageRequest.Search
                };
                var customerRecords = new RetrieveCustomerLoanMatdtResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerLoanMatdt(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<AlertCustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CustName,
                                                             x.Acctno,
                                                             x.Matdt,
                                                             x.BalQd,
                                                             x.Cif
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AlertCasa(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerAlertCasaRequest request = new RetrieveCustomerAlertCasaRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    TextSearch = pageRequest.Search
                };
                var customerRecords = new RetrieveCustomerAlertCasaResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerAlertCasa(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<AlertCustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CustName,
                                                             x.AvgbalQdN4,
                                                             x.AvgbalQdN3,
                                                             x.AvgbalQdN2,
                                                             x.AvgbalQdN1
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AlertDepositMatdt(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveTblAlertDepositMatdtRequest request = new RetrieveTblAlertDepositMatdtRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    TextSearch = pageRequest.Search
                };
                var customerRecords = new RetrieveTblAlertDepositMatdtResponse();
                customerRecords = serviceClient.Proxy.RetrieveTblAlertDepositMatdt(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<TblAlertDepositMatdtRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CustomerName,
                                                             x.Acctno,
                                                             x.Term,
                                                             x.Matdt.ToShortDateString(),
                                                             x.BalQd,
                                                             x.BranchCode
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion
        #region Celebration
        public ActionResult Celebration()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult CelebrationOfCompanyData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerCelebrationRequest request = new RetrieveCustomerCelebrationRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    Region = DataRegion.RegionMap(CurrentUserRecord.Zone),
                    TextSearch = pageRequest.Search
                };
                var customerRecords = new RetrieveCustomerCelebrationResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerCelebration(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.BirthIncorpDate.ToShortDateString(),
                                                             x.Address
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CelebrationOfRepresentativeData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerRepresentativeCelebrationRequest request = new RetrieveCustomerRepresentativeCelebrationRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = CurrentUserRecord.Dao,
                    Region = DataRegion.RegionMap(CurrentUserRecord.Zone),
                    TextSearch = pageRequest.Search
                };
                var customerRecords = new RetrieveCustomerRepresentativeCelebrationResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerRepresentativeCelebration(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRepresentativeRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.RepresentativeName,
                                                             x.CustName,
                                                             x.Position,
                                                             x.CelebrationDate
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion
        #region Top 10
        public ActionResult Top10()
        {
            return View();
        }
        [Authorize]
        [HttpPost]
        public ActionResult Top10Data(DataTablesPageRequest pageRequest, string type)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerTop10Request request = new RetrieveCustomerTop10Request
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 10,
                        PageIndex = 1
                    },
                    Dao = CurrentUserRecord.Dao,
                    Type = int.Parse(type)
                };
                var customerRecords = serviceClient.Proxy.RetrieveCustomerTop10(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.CIF,
                                                             x.Name,
                                                             x.VpbIndustry,
                                                             x.Toi
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region Ajax Methods
        [Authorize]
        [HttpPost]
        public ActionResult CustomerI2BDetailData(DataTablesPageRequest pageRequest, string cif)
        {
            List<CustomerI2BDetail> resultCustomer = new List<CustomerI2BDetail>();
            DataTableResponse pageResponse = null;
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerI2bDetailByCifRequest request = new RetrieveCustomerI2bDetailByCifRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Cif = cif,
                };
                var customerRecords = new RetrieveCustomerI2bDetailByCifResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerI2bDetailByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerI2BDetail>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{ 
                                                             cif,
                                                             x.UserName,
                                                             x.Status
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult ListGroupItem(string type, string product)
        {
            List<ListProductRecord> resultCustomer = new List<ListProductRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveListProductRequest request = new RetrieveListProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Dao = CurrentUserRecord.Dao,
                    Type = product,
                    Group = type
                };
                var customerRecords = new RetrieveListProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveListProduct(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ListProductRecord>() : customerRecords.Records;
            }
            JsonResult result = new JsonResult();
            switch (product)
            {
                case "deposit":
                    var objectItem = resultCustomer.Select(x => new
                    {
                        id = x.ProductDeposit,
                        text = x.ProductDeposit
                    });
                    result = Json(objectItem);
                    break;
                case "loan":
                    var objectLoanItem = resultCustomer.Select(x => new
                   {
                       id = x.ProductLoan,
                       text = x.ProductLoan
                   });
                    result = Json(objectLoanItem);
                    break;
                default:
                    break;
            }

            return result;
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustomerInfo(string cif)
        {
            List<CustomerInfoRecord> resultCustomer = new List<CustomerInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveCustomerInfoRequest request = new RetrieveCustomerInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveCustomerInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerInfoRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                address = x.Address,
                custName = x.CustName,
                cif = cif,
                email = x.Email,
                phone = x.Phone,
                office = x.Office,
                landLine = x.LandLine,
                fax = x.Fax,
                industry = x.VbpIndustry
            });
            return Json(objectItem);
        }

        [Authorize]
        [HttpPost]
        public ActionResult NewLending(DataTablesPageRequest pageRequest, string cif)
        {
            DataTableResponse pageResponse = null;
            List<NewLendingRecord> resultCustomer = new List<NewLendingRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveNewLendingInfoRequest request = new RetrieveNewLendingInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Cif = cif
                };
                var customerRecords = new RetrieveNewLendingInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveNewLendingInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<NewLendingRecord>() : customerRecords.Records;
            }
            pageResponse = new DataTableResponse
            {
                sEcho = pageRequest.Echo,
                sColumns = pageRequest.ColumnNames,
                aaData = resultCustomer.Select(x => new object[]{ 
                                                             x.Cif,
                                                                x.CustName,
                                                                x.ChuongTrinhSp,
                                                                x.DistDate,
                                                                x.Acctno,
                                                                x.MatDate
                    }).ToList()
            };
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RepreInfo(string repreCif)
        {
            List<RelCusInfoRecord> resultCustomer = new List<RelCusInfoRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetrieveRelCusInfoRequest request = new RetrieveRelCusInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    RelCif = repreCif
                };
                var customerRecords = new RetrieveRelCusInfoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRelCusInfoByCif(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RelCusInfoRecord>() : customerRecords.Records;
            }
            var objectItem = resultCustomer.Select(x => new
            {
                address = x.RelCusAddress,
                custName = x.RelCusName,
                cif = repreCif,
                email = x.RelCusEmail,
                phone = x.RelCusMobile,
                gender = x.RelCusGender,
                position = x.RelCusPosition
            });
            return Json(objectItem);
        }
        #endregion

        #region Helper Methods

        private CustomerPortfolioViewModel CountCustomerByGroup(bool isNew = false)
        {
            CustomerPortfolioViewModel result = new CustomerPortfolioViewModel();
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                RetrieveCustomerCountByGroupRequest request = new RetrieveCustomerCountByGroupRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerPortfolio"
                    },
                    Dao = CurrentUserRecord.Dao,
                    IsNew = isNew
                };
                var listGroupRecords = serviceClient.Proxy.RetrieveCustomerCountByGroup(request);
                result.listGroupRecord = (listGroupRecords == null || listGroupRecords.Records == null) ? new List<CustomerGroupCountRecord>() : listGroupRecords.Records;
            }
            return result;
        }

        #endregion
    }
}
